<?php
namespace common\models\notice;

use cms\modules\v1\models\Common;
use common\models\im\Im;
use common\models\Status;
use common\models\user\User;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "goods".
 *
 * @property int $id
 * @property string $name
 */
class Notice extends Common
{
    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%notice}}';
    }

    /**
     * 发送消息
     * @param array $info
     */
    public static function send($info = [])
    {
            $where = $andWhere = [];
            $userObject = User::getObjectByWhere($where,['id','tx_code']);

            $config = static::getCommonByKey('system_user_id');
            if ($config) {
                $imModel = new Im();
                foreach ($userObject->batch(499) as $users) {
                    $userMessage = [];
                    $userMessage['message_type'] = (int) Status::MESSAGE_TYPE_USER_SYSTEM_NOTICE;
                    $userMessage['type'] = (int) Status::MESSAGE_TYPE_USER_SYSTEM_NOTICE;
                    $userMessage['title'] = $info['title'] ?: '';
                    $userMessage['text'] = $info['content'] ?: '';
                    $userMessage['text_type'] = (string) $info['type'];
                    $userMessage['image'] = $info['image'] ? static::getImages($info['image']) : '' ;
                    $userMessage['time'] = date('Y-m-d H:i:s');

                    $userTxCodes = ArrayHelper::getColumn($users,'tx_code');
                    $imModel -> sendUserMessage($userTxCodes, $userMessage, $config, true);
                }
            }

    }
}
